#include "stdafx.h"
#include "ProgressDlg.h"

CProgressDlg::CProgressDlg() : CDialogEx(CProgressDlg::IDD)
{
    m_dwNativeThreadID = GetCurrentThreadId();
}

void CProgressDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CProgressDlg, CDialogEx)
    ON_MESSAGE(WM_APPEND_STRING,&CProgressDlg::OnAppendString)
END_MESSAGE_MAP()

LRESULT CProgressDlg::OnAppendString( WPARAM wParam, LPARAM lParam )
{
    SetTextMessage( (LPCSTR)wParam, lParam );
    free( (void*)wParam );
    return 0;
}

void CProgressDlg::SetTextMessage( LPCSTR lpszText, int iProg )
{
    if( !IsWindow( m_hWnd ) )
        return;

    if( GetCurrentThreadId() != m_dwNativeThreadID )
    {
        PostMessage( WM_APPEND_STRING, (WPARAM)_strdup( lpszText ), iProg );
        return;
    }

    CStatic *pStatic = (CStatic *)GetDlgItem( IDC_STATIC_MSG );
    if( pStatic )
        pStatic->SetWindowText( lpszText );

    CProgressCtrl *pProg = (CProgressCtrl *)GetDlgItem( IDC_PROGRESS_CTRL );
    if( pProg )
        pProg->SetPos( iProg );
}

void CProgressDlg::SetProgress( int iProg )
{
    if( iProg > 100 )
        return;

    CProgressCtrl *pCtrl = (CProgressCtrl *)GetDlgItem( IDC_PROGRESS_CTRL );
    pCtrl->SetPos( iProg );
}

BOOL CProgressDlg::OnInitDialog()
{
    CProgressCtrl *pCtrl = (CProgressCtrl *)GetDlgItem( IDC_PROGRESS_CTRL );
    pCtrl->SetRange32( 0, 100 );
    return TRUE;
}

void CProgressDlg::CloseDialog()
{
    this->PostMessage( WM_CLOSE );
}

void CProgressDlg::OnOk()
{
    // do nothing
}

void CProgressDlg::OnCancel()
{
    CDialogEx::OnCancel();
}
